package com.senven.address.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.senven.address.mapper.AddressMapper;
import com.senven.address.service.AddressService;

import com.senven.common.entity.Address;
import com.senven.common.pojo.vo.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author YSH
 * @since 2022-09-05
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    //添加地址
    //根据token查询出用户id
    @Override
    public CommonResult insertAddress(String token,Address address) {
        String s = redisTemplate.opsForValue().get(token);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject user = (JSONObject) map.get("user");
        String id = user.getString("id");
        address.setUserId(id);
        int insert = addressMapper.insert(address);
        return new CommonResult(2000,"添加成功",insert);
    }


    @Override
    public List<Address> findAllAddressByUserid(String id) {
        List<Address> allAddress = addressMapper.findAllAddressByuserid(id);
        return allAddress;
    }

    //根据登录的用户id查询用户的地址
    @Override
    public CommonResult findAllAddress(String token) {
        String s = redisTemplate.opsForValue().get(token);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject user = (JSONObject) map.get("user");
        String id = user.getString("id");
        List<Address> allAddress = addressMapper.findAllAddress(id);
        return new CommonResult(2000,"查询成功",allAddress);
    }

    @Override
    public CommonResult delAddress(Integer id) {
        int i = addressMapper.deleteById(id);
        return new CommonResult(2000,"删除成功",i);
    }

    @Override
    public CommonResult findAddressByid(Integer id) {
        Address address = addressMapper.selectById(id);
        return new CommonResult(2000,"查询成功",address);
    }

    @Override
    public CommonResult updateAddressByid(Address id) {
        int updateById = addressMapper.updateById(id);
        return new CommonResult(2000,"修改成功",updateById);
    }

   /* @Override
    public CommonResult updateAddress(Integer id) {
        int i = addressMapper.updateAddress(id);
        return new CommonResult(2000,"修改成功",i);
    }*/


}
